<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../../js/babel.min.js"></script>
    <script src="../../js/react.development.js"></script>
    <script src="../../js/react-dom.development.js"></script>
    <script src="../../js/prop-types.js"></script>
</head>

<body>
    <div id="example1"></div>
    <div id="example2"></div>
</body>

</html>
<script type="text/babel">
class Life extends React.Component{
   constructor(props){
       super(props)
       this.state={
           opacity:1
       }
       this.changeHandler=this.changeHandler.bind(this);
   }
   changeHandler(){
       //移除某个组件
       ReactDOM.unmountComponentAtNode(document.getElementById('example1'))
   }
    render(){
        const {opacity}=this.state;
        return <div>
           <h2 style={{opacity}}>{this.props.msg}</h2>
           <button onClick={this.changeHandler}>呵呵</button>
        </div>
    }
    
    //组件将要被移除
    componentWillUnmount() {
        //清理定时器
        clearInterval(this.interval)
    }
    
    //组件挂载完毕
    componentDidMount() {
        this.interval=setInterval(() => {
            let {opacity}=this.state;
            opacity-=0.1
            if (opacity<=0) {
                opacity=1
            }
            this.setState({opacity})
        }, 200);
    }
    
}

ReactDOM.render(<Life msg="react 太难了"/>,document.getElementById('example1'))

</script>